﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Smled
{
    public class INIGroup
    {
        public Dictionary<string, string> vals = null;
        public String title;

        public INIGroup(string Title)
        {
            title = Title;
            vals = new Dictionary<string, string>();
        }

        public INIGroup(StreamReader s)
        {
            String title = s.ReadLine();

            if (title.StartsWith("[") && title.EndsWith("]"))
            {
                this.title = title.Substring(1, title.Length - 2);
            }
            else
            {
                throw new Exception("No more groups");
            }

            vals = new Dictionary<string, string>();
            while (!s.EndOfStream)
            {
                String tmp = s.ReadLine();
                if (tmp.Length < 3) break;
                String[] parts = tmp.Split(new char[] { '=' });
                vals.Add(parts[0], parts[1]);
            }
        }

        public void WriteToStream(StreamWriter sw)
        {
            sw.WriteLine("[" + title + "]");
            foreach(KeyValuePair<string, string> kvp in vals.ToList())
                sw.WriteLine(kvp.Key + "=" + kvp.Value);
            sw.WriteLine();
        }

        public void set(String key, String val)
        {
            vals[key] = val;
        }

        public int getInteger(String key)
        {
            return getInteger(key, 0);
        }

        public int getInteger(String key, int def)
        {
            String s = vals[key];
            if (s == null) return def;
            try
            {
                int n = Int32.Parse(s);
                return n;
            }
            catch (Exception)
            {
                return def;
            }
        }

        public String getString(String key)
        {
            return getString(key, "");
        }

        public String getString(String key, String def)
        {
            String s = vals[key];
            if (s == null)
                return def;
            else
                return s;
        }

        public bool getBoolean(String key, bool def)
        {
            String s = vals[key];
            if (s != null)
                return "True" == s;
            else
                return def;
        }

        public string[] getStringList(string key)
        {
            string s = vals[key];
            string[] parts = s.Split(new char[] { ';' });
            return parts;
        }

        public int[] getIntList(string key, int def)
        {
            string s = vals[key];

            if (!s.Contains(";"))
            {
                try
                {
                    return new int[] { Int32.Parse(s) };
                }
                catch (Exception) { return new int[] {def}; }
            }

            string[] parts = s.Split(new char[] { ';' });
            int[] nums = new int[parts.Length];
            int i = 0;
            foreach(string p in parts)
            {
                try {
                nums[i] = Int32.Parse(p);
                }
                catch(Exception) { nums[i] = def; }
                i++;
            }
            return nums;
        }

        public bool AppendToFile(string FileName)
        {
            StreamWriter sw = new StreamWriter(FileName, true);
            WriteToStream(sw);
            sw.Close();
            return true;
        }
    }
}
